package com.py.credit.service.impl;

import com.py.credit.service.LhjcWriteLogsService;
import org.springframework.stereotype.Service;

import java.io.*;

@Service
public class LhjcWriteLogsServiceImpl implements LhjcWriteLogsService {

    @Override
    public void writeLogFile(String absolutepath, String batchId, String mess) {
        String path = absolutepath + File.separator + "logs" + builtPath(batchId);

        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File(path + File.separator + batchId + ".log");
        //用FileOutputSteam包装文件，并设置文件可追加
        OutputStream out = null;
        try {
            out = new FileOutputStream(f, true);
            out.write(mess.getBytes());
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String builtPath(String batchId) {
        String[] arrays = batchId.split("-");
        StringBuilder paths = new StringBuilder(50);
        paths.append(File.separator);
        paths.append(arrays[1]);
        paths.append(File.separator);
        paths.append(arrays[2]);
        paths.append(File.separator);
        paths.append(arrays[3]);
        return paths.toString();
    }

    @Override
    public String readLogFilePath(String absolutepath, String batchId) {
        return absolutepath + File.separator + "logs" + builtPath(batchId) + File.separator + batchId + ".log";
    }
}
